package com.maddox.relax.camera

import android.content.Context
import android.media.*
import android.net.Uri
import com.maddox.relax.util.AppUtils
import com.maddox.relax.util.PBitmapUtils
import java.io.File

/**
 * 音视频相关帮助类
 */
object MediaHelper {

    var audioRecorder: MediaRecorder? = null

    /**
     * 播放res/raw/ 下的音频文件
     * @param complete 播放完成的监听
     */
    fun playRawAudio(context: Context, sourceId: Int, complete: () -> Unit) {
        var mediaPlayer = MediaPlayer.create(context, sourceId)
        mediaPlayer.start()
        mediaPlayer.setOnCompletionListener {
            mediaPlayer.release()
            complete()
        }
    }

    /**
     * 播放本地下的音频文件
     * @param uri 本地音频文件的uri地址
     * @param complete 播放完成的监听
     */
    fun playSdCardAudio(applicationContext: Context, uri: Uri, complete: () -> Unit) {
        var mediaPlayer = MediaPlayer().apply {
            setAudioStreamType(AudioManager.STREAM_MUSIC)
            setDataSource(applicationContext, uri)
            prepareAsync()
            start()
        }
        mediaPlayer.setOnCompletionListener {
            mediaPlayer.release()
            complete()
        }
    }

    /**
     * 播放网络上的音频文件
     * @param url 网络音频文件的url地址
     * @param complete 播放完成的监听
     */
    fun playNetAudio(url: String, complete: () -> Unit) {
        var mediaPlayer = MediaPlayer().apply {
            setAudioStreamType(AudioManager.STREAM_MUSIC)
            setDataSource(url)
            prepareAsync()
            start()
        }
        mediaPlayer.setOnCompletionListener {
            mediaPlayer.release()
            complete()
        }
    }


    /**
     * 录制音频文件
     * @param startRecord 开始录制的回调
     * 默认是在 /sdcard/Android/app包名/Movies
     */
    fun startRecordAudio(context: Context, fileName: String, startRecord: () -> Unit) {
        //存储地址
        val videoFileDirectory = PBitmapUtils.getVideoFileDirectory(context)
        val path = File(videoFileDirectory, "${fileName}.mp3")
        if (audioRecorder == null) {
            audioRecorder = MediaRecorder().apply {
                setAudioSource(MediaRecorder.AudioSource.MIC)
                setOutputFormat(MediaRecorder.OutputFormat.DEFAULT)
                setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
            }
        }
        audioRecorder?.apply {
            setOutputFile(path.absolutePath)
            prepare()
            start()
            startRecord()
        }
    }

    /**
     * 停止录制音频文件
     * 回调出去音频时长
     */
    fun stopRecordAudio(filePath:String,stopRecord:(duration:String)->Unit) {

        audioRecorder?.apply {
            stop()
            release()
            val mediaPlayer = MediaPlayer().apply {
                setDataSource(filePath)
                prepare()
            }
            stopRecord(AppUtils.getVideoDuration(mediaPlayer.duration as Long))
        }
        audioRecorder = null
    }


    fun playOneVoice(context: Context, resId:Int){
        val attributes =
            AudioAttributes.Builder().setLegacyStreamType(AudioManager.STREAM_MUSIC).build()
        val soundPool = SoundPool.Builder().setMaxStreams(1).setAudioAttributes(attributes).build()
        val voiceId = soundPool.load(context,resId,1)
        soundPool.setOnLoadCompleteListener { soundPool, sampleId, status ->
            if (status==0){
                soundPool.play(voiceId,1.0f,1.0f,1,0,1.0f)
                soundPool.release()
            }
        }
    }








}